<template>
  <div class="login-phone">
    <el-form label-width="70px" :model="phone" :rules="rules" ref="formRef">
      <el-form-item label="手机号" prop="num">
        <el-input v-model="phone.num"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <div class="verification">
          <el-input v-model="phone.code"></el-input>
          <el-button type="primary" class="get-btn">获取验证码</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { rules } from '../config/phone-config'
import { ElForm } from 'element-plus'
import cache from '@/utils/cache'
export default defineComponent({
  setup() {
    const phone = reactive({
      num: null,
      code: null
    })
    const store = useStore()
    const formRef = ref<InstanceType<typeof ElForm>>()
    const clickLogin = (isRememberPswd: boolean) => {
      console.log('记住密码:' + isRememberPswd)
      console.log('手机号和验证码' + phone.num + phone.code)
      formRef.value?.validate((vali) => {
        if (vali) {
          console.log('验证通过了')
          if (isRememberPswd) {
            cache.setCache('phone', phone)
          } else {
            cache.deleteCache('phone')
          }
          store.dispatch('login/phoneLoginAction', { ...phone })
        } else {
          console.log('验证失败')
        }
      })
    }
    return {
      phone,
      rules,
      clickLogin,
      formRef
    }
  }
})
</script>

<style scoped lang="less">
.login-phone {
  .verification {
    display: flex;

    .get-btn {
      margin-left: 8px;
    }
  }
}
</style>
